<template>
  <div class="box">
    <div><h1>登录界面</h1></div>
    <div>
      <el-form :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="登录账号">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="form.userpass" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Login } from "../api/api";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const form = ref({
  mobile: "",
  nickname: "",
  token: "",
  username: "",
  userpass: "",
});

const onSubmit = () => {
  Login(form.value).then((res) => {
    if (res.data.code == 200) {
      let user = res.data.data;
      let token = user.token;
      sessionStorage.setItem("token", token);
      sessionStorage.setItem("user", JSON.stringify(user));
      ElMessage.success("登录成功");
      router.push("/home/order");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
</script>

<style scoped></style>
